﻿using System;
using System.Collections.Generic;
using System.Text;
using Av.Utils;
using System.Net;
using System.Collections.Specialized;

namespace SiteStatusTracker
{

    /// <summary>
    /// Stores and sends messages
    /// </summary>
    internal class Informer
    {

        /// <summary>
        /// Stores sites to be sent
        /// </summary>
        protected Dictionary<string, TrackerItem> m_arSites = new Dictionary<string, TrackerItem>();

        /// <summary>
        /// Last datetime when information was sent
        /// </summary>
        DateTime m_lastInformationAt = DateTime.MinValue;

        /// <summary>
        /// Gets/sets necessary delay between information sending (seconds)
        /// </summary>
        public int InformationDelayS { get; set; }

        public Informer()
        {
            InformationDelayS = 60 * 60;
        }

        public void Add(TrackerItem site)
        {
            lock( this )
            {
                if( !m_arSites.ContainsKey(site.Name) )
                    m_arSites[site.Name] = site;
            }
        }

        public void Inform()
        {
            TimeSpan ts = DateTime.Now - m_lastInformationAt;
            Log4cs.Log("Last time information was sent at: {0}", (m_lastInformationAt == DateTime.MinValue) ? "NEVER" : m_lastInformationAt.ToString("yy-MM-dd HH:mm"));
            
            // If we send something
            if( ts.TotalSeconds < InformationDelayS )
            {
                Log4cs.Log(Importance.Warning, "Information was sent {0}s ago. Delay should be {1}s", ts.TotalSeconds, InformationDelayS);
                return;
            }

            Send();
        }

        public void ForceInform()
        {
            Send();
        }

        protected void Send()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                lock( this )
                {
                    if( m_arSites.Count > 0 )
                    {
                        foreach( KeyValuePair<string, TrackerItem> kvp in m_arSites )
                        {
                            sb.AppendLine(kvp.Value.ToString());
                            sb.AppendLine();
                        }

                        m_arSites.Clear();
                    } else
                    {
                        Log4cs.Log(Importance.Warning, "Nothing to send...");
                    }

                }  // END LOCK

                if( sb.Length > 0 )
                {
                    Log4cs.Log("Sending to vinform.php:{0}{1}", Environment.NewLine, sb.ToString());
                    WebClient wc = new WebClient();
                    //wc.Headers.
                    NameValueCollection arValues = new NameValueCollection();
                    arValues.Add("data", sb.ToString());
                    wc.UploadValues("http://www.knygynai.lt/test/vinform.php", "POST", arValues);
                }

            } catch( Exception ex )
            {
                Log4cs.Log(Importance.Error, "Error sending information!");
                Log4cs.Log(Importance.Debug, ex.ToString());
                
            }            
        }

    }
}
